import React from 'react';
import { connect } from 'react-redux';
import {
    StyleSheet,
    Text,
    TextInput,
    View,
    ScrollView,
    Image,
    InteractionManager,
    TouchableHighlight,
    Modal,
    Alert,
} from 'react-native';

import BaseNaviLayout from '../../component/BaseNaviLayout'

import styles from './styles'

import {updateNotice} from "./actions"

import {changeShopInfo} from '../main/actions'

import { toast } from '../../utils/NativeUtil'

class NoticeEdit extends React.Component {

    constructor(props) {
        super(props);
        this.state = {text: ''};
    }

    submit = () => {
        let shopNotice = this.state.text;
        // if (shopNotice == '') {
        //    shopNotice = this.props.shopInfo.notice;
        // }

        if (shopNotice.length > 100) {
            toast("字数" + shopNotice.length.toString() + ",不能超过100");
            return
        }

        const params = {
            token:this.props.token,
            shopId:this.props.shopInfo.shopid,
            notice:shopNotice,
        }

        let shop = Object.assign({},this.props.shopInfo,{notice:shopNotice});

        this.props.updateNotice(params,()=> {

            this.props.changeShopInfo(shop)

            this.props.navigator.pop();
        })
    }

    render = () => {
        return <BaseNaviLayout
            title="公告编辑"
            leftAction={() => { this.props.navigator.pop() }}
            rightTitle="保存"
            rightAction={this.submit}
        >
        <View style={{padding:29,
            backgroundColor: 'white',
            borderBottomColor: '#000000',
            borderBottomWidth: 1}}>
        <TextInput
            ref="noticeInput"
            defaultValue={this.props.shopInfo.notice}
            placeholder="请输入公告"
            editable={true}
            multiline = {true}
            numberOfLines={0}
            onChangeText={(text) => this.setState({text})}
            underlineColorAndroid={'transparent'}
            style={{padding:0,height:200,fontSize:16}} />
        </View>    
        </BaseNaviLayout>
    }
}

const mapStateToProps = state => ({
    token: state.mainmodel.token,
    shopInfo: state.mainmodel.shopInfo, 
});

const mapDispatchToProps = dispatch => ({
    updateNotice: (params,callBack) => {
        dispatch(updateNotice(params,callBack))
    },
    changeShopInfo:(shop) => {
         dispatch(changeShopInfo(shop))
    }
});

export default connect(
    mapStateToProps,
    mapDispatchToProps
)(NoticeEdit);
